﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using HRM.HRMServiceProxy;
using Microsoft.Reporting.WinForms;

namespace HRM
{
    public partial class frmIssueInsBookList : DockContent
    {
        int pageSize = 300;//UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int _IBID = 0;
        int _department_ID;
        int _branch_ID;
        int _month = DateTime.Now.Month;
        int _year = DateTime.Now.Year;
        bool _loadedData = false;
        IssueInsBookClient _wcfIB;
        BranchClient wcfBranch;
        DepartmentClient wcfDepartment;
        DoiSoClient wcfDoiSo;
        RoleRightInfo listRole;
        MainServiceClient wcLWain;
        UserClient wcfUser;
        string _selectedID = ",";
        string _SoBHXH = "";

        public frmIssueInsBookList()
        {
            InitializeComponent();
            //LoadLanguage();
            wcfBranch = Service.Branch();
            wcfDepartment = Service.Department();
            _wcfIB = Service.IssueInsBook();
            wcLWain = Service.MainService();
            wcfDoiSo = Service.DoiSo();
            listRole = UIParamater.Quyen_DeNghiCapSoBH;
            wcfUser = Service.User();
            UISetting.LoadTreeView3(ref tvBranch);
            //toolStripSeparator1.Visible = false;
            BuildVoucherGrid();
            btnPrintList.Enabled = listRole.Create;
            btnPrint.Enabled = listRole.Create;            
            LayDanhSachLoaiDoiSo();

            btnChoDuyet.Visible = cbxLoaiDoiSo.ComboBox.SelectedValue.ToString() == "T";
            btnDuyet.Visible = cbxLoaiDoiSo.ComboBox.SelectedValue.ToString() == "P" && Program.isChuyenQuan;
            LoadData();
            showhide_commandbutton();
        }

        private void LayDanhSachLoaiDoiSo()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaLoaiDoiSo");
            dt.Columns.Add("TenLoaiDoiSo");

            dt.Rows.Add("T", "DS sổ tạm");
            dt.Rows.Add("P", "Chờ duyệt cấp sổ");
            dt.Rows.Add("D", "Hồ sơ đã duyệt");
            dt.Rows.Add("C", "Đã cấp sổ / đổi sổ");

            cbxLoaiDoiSo.ComboBox.DisplayMember = "TenLoaiDoiSo";
            cbxLoaiDoiSo.ComboBox.ValueMember = "MaLoaiDoiSo";
            cbxLoaiDoiSo.ComboBox.DataSource = dt;
            cbxLoaiDoiSo.ComboBox.SelectedValue = "T";
        }

        private void LoadLanguage()
        {
            if (this.Tag != null)
                this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
        private void BuildVoucherGrid()
        {
            dgvIssueInsBookList.Columns.Clear();
            //Thêm cột Checkbox
            DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
            col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            col.Name = "Check";
            col.HeaderText = "Chọn";
            col.Width = 50;
            col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvIssueInsBookList.Columns.Add(col);
            col.Visible = true;
            
            UISetting.AddColumn(ref dgvIssueInsBookList, "RowNumber", "STT", "", 30, true, true);
            UISetting.AddColumn(ref dgvIssueInsBookList, "Employee_ID", "Mã NV", "", 30, false, true);
            UISetting.AddColumn(ref dgvIssueInsBookList, "Employee_Code", "Mã NV", "", 80, false, true);
            UISetting.AddColumn(ref dgvIssueInsBookList, "Employee_FullName", "Họ và tên", "", 150, true, true);
            UISetting.AddColumn(ref dgvIssueInsBookList, "Employee_DateOfBirth", "Ngày sinh", "", 90, true, true);
            UISetting.AddColumn(ref dgvIssueInsBookList, "Employee_SIBookNo", "Số sổ tạm", "", 120, true, true);
            UISetting.AddColumn(ref dgvIssueInsBookList, "Employee_SIBookNo_doi", "Số sổ BHXH", "", 120, true, true);
            UISetting.AddColumn(ref dgvIssueInsBookList, "Position_Name", "Chức vụ", "", 150, true, true);

            UISetting.AddColumn(ref dgvIssueInsBookList, "DoiSo_DenNgay", "Cấp sổ đến ngày", "", 150, true, true);
        }
        void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        private void LoadData()
        {
            string keyWord = "";
            if (txtKeyWord.Text.Trim().Contains("'"))
                keyWord = txtKeyWord.Text.Trim().Replace("'", "''");
            else
                keyWord = txtKeyWord.Text.Trim();

            DataSet _ds = _wcfIB.IssueInsBook_GetList(out itemCount, _month, _year, _branch_ID, _department_ID, keyWord, currPage, pageSize,cbxLoaiDoiSo.ComboBox.SelectedValue.ToString());
            PageIndex();
            dgvIssueInsBookList.Rows.Clear();
            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvIssueInsBookList.Rows.Insert(i, 1);
                dgvIssueInsBookList.Rows[i].Cells["RowNumber"].Value = _ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvIssueInsBookList.Rows[i].Cells["Employee_ID"].Value = _ds.Tables[0].Rows[i]["Employee_ID"].ToString();
                dgvIssueInsBookList.Rows[i].Cells["Employee_Code"].Value = _ds.Tables[0].Rows[i]["Employee_Code"].ToString();
                dgvIssueInsBookList.Rows[i].Cells["Employee_FullName"].Value = _ds.Tables[0].Rows[i]["Employee_FullName"].ToString();
                dgvIssueInsBookList.Rows[i].Cells["Employee_DateOfBirth"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy");
                dgvIssueInsBookList.Rows[i].Cells["Employee_SIBookNo"].Value = _ds.Tables[0].Rows[i]["Employee_SIBookNo"].ToString();
                dgvIssueInsBookList.Rows[i].Cells["Employee_SIBookNo_doi"].Value = _ds.Tables[0].Rows[i]["Employee_SIBookNo_doi"].ToString();
                dgvIssueInsBookList.Rows[i].Cells["Position_Name"].Value = _ds.Tables[0].Rows[i]["Position_Name"].ToString();

                dgvIssueInsBookList.Rows[i].Cells["DoiSo_DenNgay"].Value =  _ds.Tables[0].Rows[i]["DoiSo_DenNgay"] != DBNull.Value ? ((DateTime)_ds.Tables[0].Rows[i]["DoiSo_DenNgay"]).ToString("MM/yyyy") : "";
                
                if (_selectedID.Contains("," + _ds.Tables[0].Rows[i]["Employee_ID"].ToString() + ","))
                    dgvIssueInsBookList.Rows[i].Cells["Check"].Value = true;
                else if (ChkCheckAll.Checked)
                {
                    _selectedID += dgvIssueInsBookList.Rows[i].Cells["Employee_ID"].Value.ToString() + ",";
                    dgvIssueInsBookList.Rows[i].Cells["Check"].Value = true;
                } else
                    dgvIssueInsBookList.Rows[i].Cells["Check"].Value = false;
                if(Convert.ToInt32(_ds.Tables[0].Rows[i]["quatrinhtruocday"].ToString()) > 0)
                    dgvIssueInsBookList.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;

                if (_ds.Tables[0].Rows[i]["Employee_SIBookNo_doi"].ToString() == _ds.Tables[0].Rows[i]["Employee_SIBookno"].ToString())
                {
                    dgvIssueInsBookList.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
                    dgvIssueInsBookList.Rows[i].DefaultCellStyle.SelectionBackColor = Color.Yellow;
                    dgvIssueInsBookList.Rows[i].Cells["Check"].ReadOnly = true;
                }
            }
            _loadedData = true;
        }
        private void GetSelectedID()
        {
            _SoBHXH = "";
            bool DaLaySoBHXH = false;
            dgvIssueInsBookList.EndEdit();
            foreach (DataGridViewRow row in dgvIssueInsBookList.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value))
                {
                    if (!_selectedID.Contains("," + row.Cells["Employee_ID"].Value.ToString() + ","))
                    {
                        _selectedID += row.Cells["Employee_ID"].Value.ToString() + ",";
                        if (!DaLaySoBHXH)
                        {
                            _SoBHXH = row.Cells["Employee_SIBookNo_doi"].Value.ToString();
                            DaLaySoBHXH = true;
                        }
                    }
                }
                else
                {
                    string sOld = "," + row.Cells["Employee_ID"].Value.ToString() + ",";
                    if (_selectedID.Contains(sOld))
                        _selectedID = _selectedID.Replace(sOld, ",");
                }
            }
        }
        private void btnFisrt_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            currPage = 0;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (totalPage > 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void frmIssueInsBookList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
            }
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            txtKeyWord.Text = "";
            if (e.Node.Level == 0)
            {
                if (Program.isAdmin || Program.isViewAll)
                {
                    _branch_ID = 0;
                    _department_ID = 0;
                }
                else
                    _branch_ID = Program.intBranchID;
                LoadData();
            }
            else if (e.Node.Level == 1)
            {
                _branch_ID = Convert.ToInt32(e.Node.Tag);
                _department_ID = 0;
                currPage = 0;
                LoadData();
            }
            else if (e.Node.Level >= 2)
            {
                _department_ID = Convert.ToInt32(e.Node.Tag);
                _branch_ID = Convert.ToInt32(e.Node.Parent.Tag);
                currPage = 0;
                LoadData();

            }
        }

        private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loadedData)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            _selectedID = "";
            currPage = 0;
            LoadData();
        }

        private void frmIssueInsBookList_Activated(object sender, EventArgs e)
        {
            //LoadLanguage();
            if (dgvIssueInsBookList.Columns.Count > 0)
            {
                dgvIssueInsBookList.Columns["RowNumber"].HeaderText = UpdateUI.GetString("stt");
                dgvIssueInsBookList.Columns["Employee_FullName"].HeaderText = UpdateUI.GetString("hovaten");
                dgvIssueInsBookList.Columns["Employee_Code"].HeaderText = UpdateUI.GetString("manhanvien");
                dgvIssueInsBookList.Columns["Position_Name"].HeaderText = UpdateUI.GetString("chucvu");
                //dgvIssueInsBookList.Columns["IssueInsBook_Date"].HeaderText = UpdateUI.GetString("denngay");

            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            if (dgvIssueInsBookList.SelectedRows.Count > 0)
            {
                int _EID = Convert.ToInt32(dgvIssueInsBookList.CurrentRow.Cells["Employee_ID"].Value);
                frmEmployeeInfo f = new frmEmployeeInfo(_EID, _branch_ID, _department_ID, 8, listRole);
                f.ShowDialog();
            }
        }
        private void cbmonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loadedData)
            {
                currPage = 0;
                LoadData();
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            _selectedID = "";
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần in danh sách!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (listRole.View)
            {
                //Lấy danh sách nhân viên chọn
                string fix_selectedID = _selectedID;
                if (fix_selectedID.StartsWith(","))
                    fix_selectedID = fix_selectedID.Substring(1);
                if (fix_selectedID.EndsWith(","))
                    fix_selectedID = fix_selectedID.Substring(0, fix_selectedID.Length - 1);


                //
                frmPreviewReport f = new frmPreviewReport("BHXHMau01a", new string[] { "Đề nghị cấp thẻ BHYT, BHXH", _month.ToString(), _year.ToString(),fix_selectedID });
                f.Show(this);
            }
            else
                MessageBox.Show("Bạn không có quyền xem!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void PrintToKhai(int EmployeeID, DateTime _From, DateTime _To)
        {
            LocalReport report = new LocalReport();
            Report2Printer prt = new Report2Printer();
            report.LoadReportDefinition(UISetting.GetEmbeddedFile("rptBHXH_ToKhai.rdlc"));
            ReportParameter r1, r2, r3;
            r1 = new ReportParameter("InsurancePlace", Program.strInsurancePlace);
            r2 = new ReportParameter("BranchName", Program.strBranch);
            r3 = new ReportParameter("BranchAddress", Program.strBranchAddress);
            report.SetParameters(new ReportParameter[] { r1, r2, r3 });
            prt.Landscape = false;
            EmployeeClient _wcf = Service.Employee();
            PremiumClient wcfPremium = Service.Premium();
            EmployeeInfo _Inf = new EmployeeInfo();
            _Inf = _wcf.Employee_GetInfo(EmployeeID);
            DataTable dt = wcfPremium.Premium_GetByDate(_From, _To, Program.strBranchCode, 2, _Inf.SIBookNo).Tables[0];
            report.DataSources.Add(new ReportDataSource("BaoCao_Premium", dt));

            string _sql = "";
            _sql += " SELECT * FROM (SELECT	* FROM vw_HRM_EmployeeActive) AS tmp1 LEFT OUTER JOIN ";
            _sql += " vw_HRM_LabourContract_Active ON tmp1.Employee_ID = vw_HRM_LabourContract_Active.LC_EmployeeID";
            _sql += " WHERE Employee_ID = " + EmployeeID.ToString();
            MainServiceClient wcfService = Service.MainService();
            DataTable dtEmpl = wcfService.Query(_sql).Tables[0];
            report.DataSources.Add(new ReportDataSource("BaoCao_Employee", dtEmpl));
            wcfService.Close();

            prt.Export(report);
            prt.CurrentPageIndex = 0;
            prt.Print(this);
        }
        
        private void btnInToKhai_Click(object sender, EventArgs e)
        {
            _selectedID = "";
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần in tờ khai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (listRole.View)
            {
                //frmTime frm = new frmTime(2);
                //if (frm.ShowDialog(this) == DialogResult.OK)
                //{
                DataGridViewRow dgvr = GetSelectedRow(dgvIssueInsBookList.Rows);
                

                string SoBHXH = _SoBHXH != "" ? _SoBHXH : "Chờ cấp số";
                frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH", new string[] { _selectedID, DateTime.Now.Date.ToString("dd/MM/yyyy"), dgvr.Cells["DoiSo_DenNgay"].Value.ToString(), SoBHXH });
                    f.Show(this);
                    //string[] Empl = _selectedID.Split(',');
                    //for (int i = 0; i < Empl.Length; i++)
                    //{
                    //    int _ID = 0;
                    //    if (Empl[i].Length > 0)
                    //    {
                    //        _ID = Convert.ToInt32(Empl[i]);
                    //        PrintToKhai(_ID, frm.FromDate, frm.ToDate);
                    //    }
                    //}
                //}
            }
            else
                MessageBox.Show("Bạn không có quyền xem!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private DataGridViewRow GetSelectedRow(DataGridViewRowCollection dataGridViewRowCollection)
        {
            DataGridViewRow retval = null;
            foreach (DataGridViewRow row in dataGridViewRowCollection)
                if (row.Cells["Check"].Value.ToString().ToLower() == "true")
                {
                    retval = row;
                    break;
                }
            return retval;
        }

        private void ChkCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            if (ChkCheckAll.Checked)
            {
                for (int i = 0; i < dgvIssueInsBookList.Rows.Count; i++)
                {
                    dgvIssueInsBookList.Rows[i].Cells["Check"].Value = true;
                }
                GetSelectedID();
            }
            else
            {
                for (int i = 0; i < dgvIssueInsBookList.Rows.Count; i++)
                {
                    dgvIssueInsBookList.Rows[i].Cells["Check"].Value = false;
                }
                _selectedID = "";
            }
            
        }

        private void cbxLoaiDoiSo_SelectedIndexChanged(object sender, EventArgs e)
        {
            //btnDuyet.Visible = cbxLoaiDoiSo.ComboBox.SelectedValue.ToString() == "P" && Program.isChuyenQuan;
            _selectedID = "";
            if (cbxLoaiDoiSo.ComboBox.SelectedValue.ToString() == "T")
                dgvIssueInsBookList.MultiSelect = false;
            else
                dgvIssueInsBookList.MultiSelect = true;
            showhide_commandbutton();
            LoadData();
        }

        private void btnChoDuyet_Click(object sender, EventArgs e)
        {                        
            _selectedID = "";
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần đề nghị cấp sổ!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                if (_selectedID.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Length > 1 && cbxLoaiDoiSo.ComboBox.SelectedValue.ToString() =="T")
                {
                    MessageBox.Show("Bạn chỉ được chọn lần lượt từng nhân viên để đề nghị cấp sổ!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            //Hiện frmToDate
            //DateTime toDate = DateTime.Now;
            //frmToDate frmToDate = new frmToDate();
            
            //DialogResult result = frmToDate.ShowDialog();
            //if (result == DialogResult.OK || result == DialogResult.Yes)
            //{
            //    toDate = frmToDate.ToDate;
            //    wcfDoiSo.HRM_Doiso_CapNhatDeNghiCapSo(Program.intBranchID, "P", _selectedID.StartsWith(",") ? _selectedID.Substring(1) : _selectedID, "", toDate);
            //    _selectedID = "";
            //    LoadData();
            //}
            //else
            //    return;

            //Bỏ hiện cấp sổ đến ngày
            DateTime toDate = DateTime.MinValue;
            wcfDoiSo.HRM_Doiso_CapNhatDeNghiCapSo(Program.intBranchID, "P", _selectedID.StartsWith(",") ? _selectedID.Substring(1) : _selectedID, "", toDate);
            _selectedID = "";
            LoadData();
        }

        private void btnDuyet_Click(object sender, EventArgs e)
        {
            _selectedID = "";
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần duyệt cấp sổ!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            string SoSoDauDS = "";
            frmNhapSoDauDS frm = new frmNhapSoDauDS(_selectedID.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Length);
            frm.ShowDialog();
            SoSoDauDS = frm.SoSoDauDS;
            if (SoSoDauDS != ""){
                wcfDoiSo.HRM_Doiso_CapNhatDeNghiCapSo(Program.intBranchID, "D", _selectedID, SoSoDauDS,DateTime.Now);
                _selectedID = "";
                LoadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            _selectedID = "";
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần xóa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (MessageBox.Show("Bạn có chắc chắn xóa hồ sơ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                string strDoiSoID = _selectedID;
                if (strDoiSoID.StartsWith(","))
                    strDoiSoID = strDoiSoID.Substring(1);
                if (strDoiSoID.EndsWith(","))
                    strDoiSoID = strDoiSoID.Substring(0, strDoiSoID.Length - 1);
                wcfDoiSo.HRM_DoiSo_DeleteStringID(strDoiSoID, cbxLoaiDoiSo.ComboBox.SelectedValue.ToString());
                _selectedID = "";
                LoadData();
            }
        }
        private void showhide_commandbutton()
        {
            btnDel.Visible = true;
            btnChoDuyet.Visible = true;
            btnDuyet.Visible = true;
            btnPrintList.Visible = true;
            btnCapso.Visible = true;
            switch (cbxLoaiDoiSo.ComboBox.SelectedValue.ToString())
            {
                case "T":
                    btnDel.Visible = false;
                    btnDuyet.Visible = false;
                    btnPrintList.Visible = false;
                    btnCapso.Visible = false;
                    break;
                case "P":
                    btnDel.Visible = true;
                    btnChoDuyet.Visible = false;
                    btnDuyet.Visible = Program.isChuyenQuan;
                    btnCapso.Visible = false;
                    break;
                case "D":
                    btnDel.Visible = Program.isChuyenQuan;
                    btnChoDuyet.Visible = false;
                    btnDuyet.Visible = Program.isChuyenQuan;
                    btnCapso.Visible = Program.isChuyenQuan;
                    break;
                case "C":
                    btnDel.Visible = Program.isChuyenQuan;
                    btnChoDuyet.Visible = false;
                    btnDuyet.Visible = false;
                    btnCapso.Visible = false;
                    break;
            }
        }

        private void btnCapso_Click(object sender, EventArgs e)
        {
            _selectedID = "";
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cấp sổ!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            wcfDoiSo.HRM_Doiso_CapNhatDeNghiCapSo(Program.intBranchID, "C", _selectedID.StartsWith(",") ? _selectedID.Substring(1) : _selectedID, "",DateTime.Now);
            _selectedID = "";
            LoadData();          
         
        }

        private void btnFind_Click_1(object sender, EventArgs e)
        {
            btnFind_Click(null, null);
        }

        private void dgvIssueInsBookList_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           
        }

        private void inTờKhaiMẫuMớiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _selectedID = "";
            GetSelectedID();
            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần in tờ khai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (listRole.View)
            {
                
                DataGridViewRow dgvr = GetSelectedRow(dgvIssueInsBookList.Rows);
                string SoBHXH = _SoBHXH != "" ? _SoBHXH : "Chờ cấp số";
                frmPreviewReport f = new frmPreviewReport("InToKhaiBHXH_Maumoi", new string[] { _selectedID, DateTime.Now.Date.ToString("dd/MM/yyyy"), dgvr.Cells["DoiSo_DenNgay"].Value.ToString(), SoBHXH });
                f.Show(this);                
            }
            else
                MessageBox.Show("Bạn không có quyền xem!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void inPhụLụcMẫuA01TSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            _selectedID = "";
            string _SoSoBHXH = "";
            int Counter = 0;
            //
            dgvIssueInsBookList.EndEdit();
            foreach (DataGridViewRow row in dgvIssueInsBookList.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value))
                {
                    _selectedID = row.Cells["Employee_ID"].Value.ToString();
                    _SoSoBHXH = row.Cells["Employee_SIBookNo"].Value.ToString();
                    Counter++;
                }
                if (Counter > 1)
                        break;
            }
            //

            if (_selectedID == "," || _selectedID == "")
            {
                MessageBox.Show("Bạn chưa chọn nhân viên cần in tờ khai!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if( _selectedID.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries).Length > 1)
            {
                MessageBox.Show("Bạn chỉ được chọn 1 nhân viên để in phụ lục!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (listRole.View)
            {
                frmPreviewReport f = new frmPreviewReport("InPhuLucBHXH", new string[] { _SoSoBHXH});
                f.Show(this);
            }
            else
                MessageBox.Show("Bạn không có quyền xem!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }
}
